package by.grsu.kononovich.page.simple;
import java.util.List;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.ListDataProvider;

import by.grsu.kononovich.app.MyFirstWebApplication;
import by.grsu.kononovich.dao.impl.AdministratorDao;
import by.grsu.kononovich.entity.Administrator;


public class AdministratorsListPage extends WebPage {
	public AdministratorsListPage() {
		super();

		
		final AdministratorDao adminDao = new AdministratorDao(MyFirstWebApplication.DATA_XML_FOLDER);

		
		final List<Administrator> allAdmins = adminDao.getAll();

		
		add(new DataView<Administrator>("table-rows", new ListDataProvider<Administrator>(allAdmins)) {
			@Override
			protected void populateItem(final Item<Administrator> item) {
				final Administrator admin = item.getModelObject();
				
				item.add(new Label("id", admin.getId()));
				item.add(new Label("name", admin.getName()));
				item.add(new Label("surname", admin.getSurname()));

				
				item.add(new Link("editLink") {
					@Override
					public void onClick() {
						
						setResponsePage(new AdministratorEditPage(admin));
						System.out.println("Administrator"+admin.getName()+" "+admin.getSurname()+" edit");
					}
				});

				
				item.add(new Link("deleteLink") {
					@Override
					public void onClick() {
					
						adminDao.delete(admin.getId());
						
						setResponsePage(new AdministratorsListPage());
						System.out.println("Administrator "+admin.getName()+" "+admin.getSurname()+" deleted");
					}
				});
			}
		});

		
		add(new Link("createLink") {
			@Override
			public void onClick() {
				
				setResponsePage(new AdministratorEditPage(new Administrator()));
				System.out.println("button CREAT NEW ADMINISTRSTOR pressed");
			}
		});
		}
}
